package com.kim.nidoya.activity;

import java.io.File;
import java.io.FileNotFoundException;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kim.file.FileUtils;
import com.kim.file.SDCardUtils;
import com.kim.nidoya.R;

/**
 * 注册页
 * 
 * @author kim
 * 
 */
public class RegisterActivity extends BaseActivity {

	public enum REGISTER_STEP {
		INIT, PASSWORD, HEADICON, COMPLETE;
	}

	private static final String HEADICONNAME = "nidoya.jpg";

	private static final int IMAGE_REQUEST_CODE = 0; // 选择本地图片
	private static final int CAMERA_REQUEST_CODE = 1; // 拍照
	private static final int RESULT_REQUEST_CODE = 2;

	private String[] items = new String[] { "选择本地图片", "拍照" };

	private View currentView; // 当前视图

	private Button mButtonNext; // 下一页
	private Button mButtonPre; // 上一页
	private Button mButtonJump; // 跳过
	private Button mButtonComplete; // 完成
	private Button mButtonBack; // 返回

	private RelativeLayout mLayoutSetHeadIcon; // 设置头像
	private ImageView mImageViewHeadIcon; // 头像显示
	private TextView mTextViewHeadIcon;// 无头像时显示文字
	private Drawable drawable; // 头像
	
	//用户级别
	RadioGroup group ;
	RadioButton radioButton;

	private REGISTER_STEP currentStep;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.register_layout_password);
		// setContentView(R.layout.register_layout_pic);
		// setContentView(R.layout.register_layout_complete);

	}

	@Override
	protected void initParam() {
		currentStep = REGISTER_STEP.INIT;
	}

	@Override
	protected void initView() {
		switch (currentStep) {
		case INIT:
			setContentView(R.layout.register_layout_init);
			mButtonNext = (Button) findViewById(R.id.btn_next);
			mButtonBack = (Button) findViewById(R.id.btn_back);
			mButtonNext.setOnClickListener(this);
			mButtonBack.setOnClickListener(this);
			break;
		case PASSWORD:
			setContentView(R.layout.register_layout_password);
			mButtonNext = (Button) findViewById(R.id.btn_next);
			mButtonPre = (Button) findViewById(R.id.btn_pre);
			mButtonNext.setOnClickListener(this);
			mButtonPre.setOnClickListener(this);
			break;
		case HEADICON:
			setContentView(R.layout.register_layout_pic);
			mButtonJump = (Button) findViewById(R.id.btn_jump);
			mLayoutSetHeadIcon = (RelativeLayout) findViewById(R.id.rl_headicon);
			mImageViewHeadIcon = (ImageView) findViewById(R.id.img_headicon);
			mTextViewHeadIcon = (TextView) findViewById(R.id.txt_headicon);
			mButtonJump.setOnClickListener(this);
			mLayoutSetHeadIcon.setOnClickListener(this);
			break;
		case COMPLETE:
			setContentView(R.layout.register_layout_complete);
			mButtonComplete = (Button) findViewById(R.id.btn_complete);
			mButtonComplete.setOnClickListener(this);
			mImageViewHeadIcon = (ImageView) findViewById(R.id.img_headicon);
			mTextViewHeadIcon = (TextView) findViewById(R.id.txt_headicon);
			if (drawable != null) {
				mImageViewHeadIcon.setImageDrawable(drawable);
				mImageViewHeadIcon.setVisibility(View.VISIBLE);
				mTextViewHeadIcon.setVisibility(View.GONE);
			}
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_next:
			if (currentStep.equals(REGISTER_STEP.INIT)) {
				currentStep = REGISTER_STEP.PASSWORD;
			} else if (currentStep.equals(REGISTER_STEP.PASSWORD)) {
				currentStep = REGISTER_STEP.HEADICON;
			}
			initView();
			break;
		case R.id.btn_jump:
			if (currentStep.equals(REGISTER_STEP.HEADICON)) {
				currentStep = REGISTER_STEP.COMPLETE;
			}
			initView();
			break;
		case R.id.btn_complete:
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_pre:
			currentStep = REGISTER_STEP.INIT;
			initView();
			break;
		case R.id.rl_headicon:
			showHeadIconTypeDialog();
			break;
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		// super.onBackPressed();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		// 结果码不等于取消时候
		if (resultCode != RESULT_CANCELED) {
			switch (requestCode) {
			case IMAGE_REQUEST_CODE:
				startPhotoZoom(data.getData());
				break;
			case CAMERA_REQUEST_CODE:
				if (SDCardUtils.sdcardAvailable()) {
					File tempFile = new File(SDCardUtils.getSDCardBaseDir()
							+ HEADICONNAME);
					startPhotoZoom(Uri.fromFile(tempFile));
				} else {
					Toast.makeText(this, "未找到存储卡,无法存储照片！", Toast.LENGTH_LONG)
							.show();
				}
				break;
			case RESULT_REQUEST_CODE:
				if (data != null) {
					getImageToView(data);
				}
				break;
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 头像设置对话框
	 */
	private void showHeadIconTypeDialog() {
		new AlertDialog.Builder(this).setTitle(R.string.label_set_headicon)
				.setItems(items, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0:
							Intent intentFromGallery = new Intent();
							intentFromGallery.setType("image/*");
							// 设置文件类型
							intentFromGallery
									.setAction(Intent.ACTION_GET_CONTENT);
							startActivityForResult(intentFromGallery,
									IMAGE_REQUEST_CODE);
							break;
						case 1:
							Intent intentFromCapture = new Intent(
									MediaStore.ACTION_IMAGE_CAPTURE);
							// 判断存储卡是否可以用,可用进行存储
							if (SDCardUtils.sdcardAvailable()) {
								intentFromCapture.putExtra(
										MediaStore.EXTRA_OUTPUT, Uri
												.fromFile(new File(SDCardUtils
														.getSDCardBaseDir(),
														HEADICONNAME)));
							}
							startActivityForResult(intentFromCapture,
									CAMERA_REQUEST_CODE);
							break;
						}

					}
				}).setNegativeButton(R.string.cancel, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
				}).setCancelable(true).show();
	}

	/**
	 * 裁剪图片方法实现
	 * 
	 * @param uri
	 */
	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 设置裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 320);
		intent.putExtra("outputY", 320);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, 2);
	}

	/**
	 * 保存裁剪之后的图片数据
	 * 
	 * @param picdata
	 */
	private void getImageToView(Intent data) {
		Bundle extras = data.getExtras();
		if (extras != null) {
			Bitmap photo = extras.getParcelable("data");
			drawable = new BitmapDrawable(photo);
			mTextViewHeadIcon.setVisibility(View.GONE);
			mImageViewHeadIcon.setImageDrawable(drawable);
			mImageViewHeadIcon.setVisibility(View.VISIBLE);
			// 保存头像
			try {
				FileUtils.saveBitmap2DataDir(photo, HEADICONNAME, this);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

}
